package com.toogu.mazhangmen.ui.monitor;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.toogu.mazhangmen.bean.EquipmentsBean;
import com.toogu.mazhangmen.ui.view.MonitorView;
import com.toogu.mazhangmen.util.MonitorUtil;
import com.toogu.mazhangmen.util.UiUtil;

import java.util.List;

public class MonitorAdapter extends RecyclerView.Adapter<MonitorAdapter.ViewHolder>{

    private Activity activity;
    private List<EquipmentsBean> equipmentsBeanList;
    private OnChoiceChange onChoiceChange;
    private int position=-1;
    private String devId;

   public interface OnChoiceChange{
       void change(EquipmentsBean equipmentsBean);
   }


    public MonitorAdapter(Activity activity, List<EquipmentsBean> equipmentsBeanList,String devId,OnChoiceChange onChoiceChange){
        this.activity=activity;
        this.equipmentsBeanList=equipmentsBeanList;
        this.devId=devId;
        this.onChoiceChange=onChoiceChange;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private MonitorView monitorView;
        private MonitorUtil monitorUtil;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            monitorView = (MonitorView) itemView;
            LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(UiUtil.dip2px(180), UiUtil.dip2px(180)/9*5);
            layoutParams.rightMargin=UiUtil.dip2px(2);
            monitorView.setLayoutParams(layoutParams);
        }
    }

    @Override
    public void onViewRecycled(@NonNull ViewHolder holder) {
        super.onViewRecycled(holder);
        if(holder.monitorUtil!=null)
        holder.monitorUtil.destroyMonitor();
    }

    @Override
    public void onViewAttachedToWindow(@NonNull ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        if(holder.monitorUtil!=null)
        holder.monitorUtil.startMonitor();
    }

    @Override
    public void onViewDetachedFromWindow(@NonNull ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        if(holder.monitorUtil!=null)
        holder.monitorUtil.stopMonitor();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View view = new MonitorView(activity);
        final ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        int pos = position;
        if (position >= this.position) pos++;
        EquipmentsBean equipmentsBean = equipmentsBeanList.get(pos);
        holder.monitorUtil = new MonitorUtil(activity, equipmentsBean, holder.monitorView);
        holder.monitorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onChoiceChange != null) {
                    devId = equipmentsBean.getCode();
                    MonitorAdapter.this.position = position;
                    onChoiceChange.change(equipmentsBean);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return equipmentsBeanList.size()-1;
    }

    public void notifyDataSetChange(){
        for(int i=0;i<equipmentsBeanList.size();i++){
            if(equipmentsBeanList.get(i).getCode().equals(devId)){
                position=i;
                break;
            }
        }
        this.notifyDataSetChanged();
    }

}
